package orchestration.android.execution;


import orchestration.android.ICallback;
import orchestration.android.parcelable.ParcelableAndroidTaskDTO;
import orchestration.businessprocess.execution.BPExecution;
import orchestration.businessprocess.execution.IBPExecutionListener;
import orchestration.businessprocess.model.nodes.BPEndEvent;
import orchestration.businessprocess.model.nodes.BPIntEvent;
import orchestration.businessprocess.model.nodes.BPStartEvent;
import orchestration.businessprocess.model.nodes.BPTask;
import android.os.RemoteException;

/**
 * @author  <a href="mailto:car.ferrarini@gmail.com">Carlo Ferrarini</a>
 */
public class AndroidCallback2BPExecutionListenerAdapter implements IBPExecutionListener {

	/**
	 * @uml.property  name="callback"
	 * @uml.associationEnd  
	 */
	private ICallback callback;
	public AndroidCallback2BPExecutionListenerAdapter(ICallback callback) {
		this.callback=callback;
	}
	
	@Override
	public void onEndEvent(BPEndEvent endEvent,BPExecution execution) {
		ParcelableAndroidTaskDTO dto=DTOFactory.createDTO(endEvent, execution);
		try {
			callback.onEndProcessing(dto);
		} catch (RemoteException e) {
			execution.getContainer().handleException(e);
		}
	}

	@Override
	public void onIntEvent(BPIntEvent intEvent,BPExecution execution) {
		ParcelableAndroidTaskDTO dto=DTOFactory.createDTO(intEvent, execution);
		try {
			callback.onTask(dto);
		} catch (RemoteException e) {
			execution.getContainer().handleException(e);
		}
	}

	@Override
	public void onStartEvent(BPStartEvent startEvent,BPExecution execution) {
		ParcelableAndroidTaskDTO dto=DTOFactory.createDTO(startEvent, execution);
		try {
			callback.onStartProcessing(dto);
		} catch (RemoteException e) {
			execution.getContainer().handleException(e);
		}
	}	
	@Override
	public void onTask(BPTask task,BPExecution execution) {
		ParcelableAndroidTaskDTO dto=DTOFactory.createDTO(task, execution);
		try {
			callback.onTask(dto);
		} catch (RemoteException e) {
			execution.getContainer().handleException(e);
		}
	}
}
